clear
clc
warning off;
clear
clc
warning off;

path = './';
addpath(genpath(path));
ResSavePath = '.\res\';
if(~exist(ResSavePath,'file'))
    mkdir(ResSavePath);
    addpath(genpath(ResSavePath));
end
datasetName = { 'Dermatology'};

for dataIndex = 1 : length(datasetName)
    try
        dataName =  datasetName{dataIndex};
         load(['.\dataset\',dataName ,'_shuffled.mat'],'X','Y');
        classnum = length(unique(Y));
         [k,kmax] = findk(X,classnum);
        viewnum = size(X,1);
        X1 = cell(size(X));
        samplenum = size(X{1},1);
        anchornum =classnum;%k,3k
%         k = classnum;
        featurenum = k;
        bestres=0;
        bestZZZZZ=0;  
        best_lambda = 0;
        f_num = k;
         for anchornum = [k, 2*k, 3*k] 
             for featurenum = [k, k+1, k+2]
                 for lambda = [0.1, 1, 10, 100, 1000]
                 if featurenum <= kmax
                     tic;
                    [Aset,Bset,Zset,avg_time,avg_iter,obj] = increment(X,viewnum,anchornum,samplenum,featurenum, lambda);
                    res = myNMIACCwithmean(Zset{viewnum}, Y, classnum);
                 else
                     fprintf('featurenum out');
                     continue;
                 end
                 
                    if res(1)>bestres(1)
                        bestres = res;
                        f_num = featurenum;
%                         best_std = std;
                        best_time = avg_time;
                        best_iter = avg_iter;
                        best_obj = obj;%存储目标值优化的中间值
                        bestZZZZZ = Zset{viewnum};
                        best_lambda = lambda;              
                    end
                 end
             end
         end
            f_num = f_num - k ;
            time=toc;
            resFile = [ResSavePath,dataName,'-ACC=', num2str(bestres(1)), '.mat'];
            save(resFile,'bestZZZZZ', 'f_num','bestres','Zset','Aset','Bset','best_time','best_lambda','best_iter','best_obj','time'); %%%%obj改best_obj
%           % 结果写入EXCEL
%             res = bestres;
%             rescell = {f_num,lambda,res(1),res(2),res(3),res(4),res(5),res(6),res(7),res(8)}; % 以单元格数组形式存储
%             ToExcel(dataName,res,ResSavePath)
        catch ME
             fprintf('出错: %s\n', ME.message);
            continue;
    end  
end